{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cd62e0ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 朴素贝叶斯算法【实验案例】\n",
    "# 载入分析所需要的模块和函数\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.naive_bayes import MultinomialNB\n",
    "from sklearn.naive_bayes import BernoulliNB\n",
    "from sklearn.metrics import confusion_matrix\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import cross_val_score\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "267a401c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    0.583782\n",
       "0    0.416218\n",
       "Name: V1, dtype: float64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多项式、二项式朴素贝叶斯算法示例\n",
    "# 1、数据读取及观察\n",
    "data=pd.read_csv('数据5.2.csv')\n",
    "data.V1.value_counts()#观察样本示例全集中响应变量的分类计数值\n",
    "data.V1.value_counts(normalize=True)#观察样本示例全集中响应变量的分类占比\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2844fe4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 2、将样本示例全集分割为训练样本和测试样本\n",
    "X = data.drop(['V1'],axis=1)#设置特征变量，即除V1之外的全部变量\n",
    "y = data['V1']#设置响应变量，即V1\n",
    "X_train, X_test, y_train, y_test =  train_test_split(X,y,test_size=0.3, stratify=y, random_state=123)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9b98fa6f",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.8168044077134986"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3、多项式、二项式朴素贝叶斯算法拟合\n",
    "#   多项朴素贝叶斯方法\n",
    "model = MultinomialNB(alpha=0)#采取多项朴素贝叶斯方法，不进行拉普拉斯修正\n",
    "model.fit(X_train, y_train)#基于训练样本，使用fit方进行拟合\n",
    "model.score(X_test, y_test)#基于测试样本，计算模型预测准确率\n",
    "\n",
    "model = MultinomialNB(alpha=1)#采取多项朴素贝叶斯方法，进行拉普拉斯修正\n",
    "model.fit(X_train, y_train)#基于训练样本，使用fit方进行拟合\n",
    "model.score(X_test, y_test)#基于测试样本，计算模型预测准确率\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3ca475b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6914600550964187"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#  二项朴素贝叶斯方法\n",
    "model = BernoulliNB(alpha=1)#采取二项朴素贝叶斯方法，进行拉普拉斯修正\n",
    "model.fit(X_train, y_train)#基于训练样本，使用fit方进行拟合\n",
    "model.score(X_test, y_test)#基于测试样本，计算模型预测准确率\n",
    "\n",
    "model = BernoulliNB(binarize=2, alpha=1)#采取二项朴素贝叶斯方法，设置参数binarize=2，进行拉普拉斯修正\n",
    "model.fit(X_train, y_train)#基于训练样本，使用fit方进行拟合\n",
    "model.score(X_test, y_test)#基于测试样本，计算模型预测准确率\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e09638b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((1352,), (339,), (726,))"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4、寻求二项式朴素贝叶斯算法拟合的最优参数\n",
    "X_trainval, X_test, y_trainval, y_test =  train_test_split(X, y, test_size=0.3, stratify=y, random_state=10)#随机抽取30%的样本作为测试集\n",
    "X_train, X_val, y_train, y_val =  train_test_split(X_trainval, y_trainval, test_size=0.2, stratify=y_trainval, random_state=100)#从剩余的70%的样本集（训练集+验证集）中随机抽取20%作为验证集\n",
    "y_train.shape, y_val.shape, y_test.shape#观察样本集形状，得到各样本集的样本容量。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "eb780354",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.7851239669421488"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#方法1：通过将样本分割为训练样本、验证样本、测试样本的方式寻找最优参数（要求原始数据的样本容量较大）\n",
    "best_val_score = 0\n",
    "for binarize in np.arange(0, 5.5, 0.5):\n",
    "    for alpha in np.arange(0, 1.1, 0.1):\n",
    "        model = BernoulliNB(binarize=binarize, alpha=alpha)\n",
    "        model.fit(X_train, y_train)\n",
    "        score = model.score(X_val, y_val) \n",
    "        if score > best_val_score:\n",
    "            best_val_score = score\n",
    "            best_val_parameters = {'binarize': binarize, 'alpha': alpha}\n",
    "best_val_score#计算验证集的最优预测准确率\n",
    "best_val_parameters#得到最优参数\n",
    "model = BernoulliNB(**best_val_parameters)#使用前面得到的最优参数构建伯努利朴素贝叶斯模型\n",
    "model.fit(X_trainval, y_trainval)#使用前述70%的样本集（训练集+验证集）进行伯努利朴素贝叶斯估计\n",
    "model.score(X_test, y_test) #输出基于测试样本得到的预测准确率\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b7df4f69",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.7851239669421488"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##方法2：采用交叉验证方法寻找最优参数，双层for循环+ cross_val_score\n",
    "best_score = 0\n",
    "kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=1)\n",
    "for binarize in np.arange(0, 5.5, 0.5):\n",
    "    for alpha in np.arange(0, 1.1, 0.1):\n",
    "        model = BernoulliNB(binarize=binarize, alpha=alpha)        \n",
    "        scores = cross_val_score(model, X_trainval, y_trainval, cv=kfold) \n",
    "        score = np.mean(scores)\n",
    "        if score > best_score:\n",
    "            best_score = score\n",
    "            best_parameters = {'binarize': binarize, 'alpha': alpha}\n",
    "best_score\n",
    "best_parameters\n",
    "model = BernoulliNB(**best_parameters)\n",
    "model.fit(X_trainval, y_trainval)\n",
    "model.score(X_test, y_test) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4d5a1a9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n",
      "d:\\python\\lib\\site-packages\\sklearn\\naive_bayes.py:557: UserWarning: alpha too small will result in numeric errors, setting alpha = 1.0e-10\n",
      "  % _ALPHA_MIN\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.77119039331709"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 方法3：网格搜索GridSearchCV\n",
    "param_grid = {'binarize': np.arange(0, 5.5, 0.5), 'alpha': np.arange(0, 1.1, 0.1)}#定义字典形式的参数网络\n",
    "kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=1)#保持每折子样本中响应变量各类别数据占比相同\n",
    "model = GridSearchCV(BernoulliNB(), param_grid, cv=kfold)#构建伯努利朴素贝叶斯模型，使用上步得到的参数网络，使用10折交叉验证方法进行交叉验证\n",
    "model.fit(X_trainval, y_trainval)#使用前述70%的样本集（训练集+验证集）进行伯努利朴素贝叶斯估计\n",
    "model.score(X_test, y_test)#输出基于测试样本得到的预测准确率\n",
    "model.best_params_#输出最优参数\n",
    "model.best_score_ #计算最优预测准确率\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "27512067",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[231  71]\n",
      " [ 85 339]]\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.73      0.76      0.75       302\n",
      "           1       0.83      0.80      0.81       424\n",
      "\n",
      "    accuracy                           0.79       726\n",
      "   macro avg       0.78      0.78      0.78       726\n",
      "weighted avg       0.79      0.79      0.79       726\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 5、最优二项式朴素贝叶斯算法模型性能评价\n",
    "prob = model.predict_proba(X_test)\n",
    "prob[:5]\n",
    "pred = model.predict(X_test)\n",
    "pred[:5]\n",
    "print(confusion_matrix(y_test, pred))\n",
    "print(classification_report(y_test,pred))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2275c305",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
